import methods from "./index";
import { consoleInterceptor, MessageWriter } from "./log";
import { EditorView, el, state } from "./codeMirror";
function initEditorView() {
    return new EditorView({
        state,
        parent: el!,
    });
}
function createButton(name: string, event: (e: MouseEvent) => any) {
    const el = document.createElement("button");
    el.innerHTML = name;
    el.classList.add("btn-algorithm");
    el.addEventListener("click", event);
    return el;
}
function register(button: HTMLButtonElement) {
    document.querySelector(".tree")!.append(button);
}
function install(name: string, event: (e: MouseEvent) => any) {
    register(createButton(name, event));
}
function methodsInstall(editor: EditorView) {
    methods.forEach(({ name, event, core }) => {
        install(name, (e: MouseEvent) => {
            const array = (e.target as HTMLElement).parentElement!.children;
            const currentValue = editor.state.doc.toString();
            for (let index = 0; index < array.length; index++) {
                const element = array[index];
                element === e.target ? element.classList.add("active") : element.classList.remove("active");
            }
            editor.dispatch({
                changes: { from: 0, to: currentValue.length, insert: core.toString() },
            });
            const logger = new MessageWriter(event);
            logger.writeMessage();
        });
    });
}
function logInstall() {
    consoleInterceptor(new MessageWriter(null, document.getElementById("console")!));
}
logInstall();
methodsInstall(initEditorView());
